import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/model/news.dart';
import 'package:kxy_flutter_app/view/news/news_item_widget.dart';
import 'package:kxy_flutter_app/view/root/view_model/navigation_model.dart';
import 'package:provider/provider.dart';

class HomeNewsWidget extends StatefulWidget {
  final List<News> newsList;
  final List<News> partyNewsList;
  HomeNewsWidget({
    Key key,
    @required this.newsList,
    @required this.partyNewsList,
  }) : super(key: key);

  @override
  _HomeNewsWidgetState createState() => _HomeNewsWidgetState();
}

TextStyle get checkTextStyle {
  return TextStyle(
    fontSize: 18,
    color: Colors.black87,
    fontWeight: FontWeight.bold,
  );
}

TextStyle get unCheckTextStyle {
  return TextStyle(
    fontSize: 18,
    color: Colors.black54,
    fontWeight: FontWeight.bold,
  );
}

class _HomeNewsWidgetState extends State<HomeNewsWidget> {
  int index = 0;
  List<News> newsList;

  @override
  Widget build(BuildContext context) {
    newsList = index == 0 ? widget.newsList : widget.partyNewsList;
    var header = Container(
      alignment: Alignment.centerLeft,
      margin: EdgeInsets.only(top: 20),
      padding: EdgeInsets.symmetric(horizontal: 10),
      child: Row(
        children: [
          GestureDetector(
            child: Text(
              '推荐新闻',
              style: index == 0 ? checkTextStyle : unCheckTextStyle,
            ),
            onTap: () {
              setState(() {
                index = 0;
              });
            },
          ),
          GestureDetector(
            child: Padding(
              padding: EdgeInsets.only(left: 5),
              child: Text(
                '党建新闻',
                style: index == 1 ? checkTextStyle : unCheckTextStyle,
              ),
            ),
            onTap: () {
              setState(() {
                index = 1;
              });
            },
          ),
          Expanded(child: Container()),
          moreBtn(context)
        ],
      ),
    );

    return Column(
      children: [header, body()],
    );
  }

  Widget moreBtn(BuildContext context) {
    return GestureDetector(
      child: Row(children: [
        Text(
          '查看更多',
          style: TextStyle(
            fontSize: 16,
            color: Colors.black54,
          ),
        ),
        Icon(
          Icons.chevron_right,
          size: 20,
          color: Colors.black54,
        ),
      ]),
      onTap: () {
        var navModel = Provider.of<NavigationModel>(context, listen: false);
        navModel.changeSelectIndex(1);
      },
    );
  }

  Widget body() {
    var boxShadow = [
      BoxShadow(
        color: Color.fromRGBO(240, 240, 240, 1),
        offset: Offset(1, 1),
      ),
      BoxShadow(
        color: Color.fromRGBO(240, 240, 240, 1),
        offset: Offset(-1, -1),
      ),
    ];
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5),
        color: Colors.white,
        boxShadow: boxShadow,
      ),
      margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
      child: newsListWidget(),
    );
  }

  Widget newsListWidget() {
    return Column(
      children: ListTile.divideTiles(
        tiles: newsList.map((news) {
          return ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: NewsItemWidget(
              news: news,
              padding: EdgeInsets.symmetric(horizontal: 8, vertical: 10),
            ),
          );
        }).toList(),
        context: context,
        color: Colors.black12,
      ).toList(),
    );
  }
}
